package sk.velkavrana.app.databaseInfo.dao.xml;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import sk.velkavrana.app.databaseInfo.dao.DatabaseInfoDao;
import sk.velkavrana.app.databaseInfo.model.DatabaseInfo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mtymes
 * @since Sep 21, 2009 11:30:47 PM
 */
public class DatabaseInfoDaoXml extends AbstractDaoXml<DatabaseInfo, String> implements DatabaseInfoDao {

    protected DatabaseInfo getFromXml(String fileName) {
        DatabaseInfo databaseInfo = null;

        SAXReader reader = new SAXReader();
        try {
            File file = new File(fileName);

            if (file.exists()) {
                Document document = reader.read(file);

                Element root = document.getRootElement();

                if ("db".equals(root.getName())) {
                    databaseInfo = new DatabaseInfo();

                    databaseInfo.setId(file.getName().split("\\.")[0]);
                    databaseInfo.setName(root.attribute("name").getValue());
                    databaseInfo.setDriver(root.attribute("driver").getValue());
                    databaseInfo.setDialect(root.attribute("dialect").getValue());
                    databaseInfo.setTemplateUrl(root.attribute("templateUrl").getValue());

                    @SuppressWarnings("unchecked")
                    List<Element> jarFileElements = root.elements("jarFile");

                    if (jarFileElements != null) {
                        List<String> jarFiles = new ArrayList<String>();
                        for (Element jarFileElement : jarFileElements) {
                            String jarFile = jarFileElement.getText();

                            if (StringUtils.isNotBlank(jarFile)) {
                                jarFiles.add(jarFile);
                            }
                        }
                        databaseInfo.setJarFiles(jarFiles);
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            databaseInfo = null;
        }

        return databaseInfo;
    }


    public void save(DatabaseInfo bean) {
        Document document = DocumentHelper.createDocument();

        Element root = document.addElement("db");

        root.addAttribute("name", bean.getName());
        root.addAttribute("driver", bean.getDriver());
        root.addAttribute("dialect", bean.getDialect());
        root.addAttribute("templateUrl", bean.getTemplateUrl());

        List<String> jarFiles = bean.getJarFiles();
        if (jarFiles != null) {
            for (String jarFile : jarFiles) {
                Element jarFileElement = root.addElement("jarFile");
                jarFileElement.addText(jarFile);
            }
        }

        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer =
                    new XMLWriter(
                            new FileWriter(getFullFileName(bean.getId())),
                            format
                    );
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
